<template>
    <div>
        <van-nav-bar title="优惠券" left-text="返回" left-arrow @click-left="onClickLeft" />
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh()">
            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad()"
                :immediate-check="false">
                <van-coupon-list :coupons="coupons" :chosen-coupon="chosenCoupon" :disabled-coupons="disabledCoupons"
                    @exchange="onExchange" @change="onChange" :show-close-button="clobutton" close-button-text="w" />
            </van-list>
        </van-pull-refresh>
    </div>
</template>
<script>
// const coupon = {
//     condition: '无使用门槛\n最多优惠12元',
//     reason: '',
//     value: 150,
//     name: '优惠券名称',
//     startAt: 1489104000,
//     endAt: 1514592000,
//     valueDesc: '1.5',
//     unitDesc: '元',
// };
export default {
    data() {
        return {
            showList: true,
            chosenCoupon: -1,
            coupons: [],
            disabledCoupons: [],
            clobutton: false,
            couponlist: '',
            loading: true,
            finished: false,
            refreshing: false,
            total: '',
            // list:[],
            page: 1,
        }
    },
    methods: {
        onClickLeft() {
            this.$router.back()
        },
        async onExchange(code) {
            let res = await this.$frontaxios.post(this.$front.changeDhm, { dhm: code });
            if (res.code == 1) {
                this.$toast.success('兑换优惠券成功!');
                location.reload();
            } else {
                this.$toast.fail(res.msg);
            }
        },
        onChange(id) {
            if (this.coupons[id].info_type == 'column') {
                this.$router.push({ name: "product", query: { id:this.coupons[id].k_id } });
            } else if (this.coupons[id].info_type == 'knowledge') {
                this.toOrderOne(this.coupons[id].k_id)
            }
        },
        async getListCoupon() {
            let query = {
                page: 1,
                limit: 10,
                keyword: ''
            };
            query.page = this.page;
            let res = await this.$frontaxios.post(this.$front.getUserAllCoupon, query);
            if (res.code == 1) {
                this.loading = true;
                this.couponlist = res.data.list;
                // this.nocouponlist=res2.data.list;
                this.total = res.data.total;
                //  this.total2=res2.data.total
            } else {
                console.log('没有获取到兑换课程')
            }
        },
        async getListNoCoupon() {
            //  let query = {
            //             page: 1,
            //             limit: 10,
            //             keyword: ''
            //         };
            let res2 = await this.$frontaxios.get(this.$front.getUserAllNoCoupon);
            if (res2.code == 1) {
                this.disabledCoupons = res2.data.list;
            }
        },
        async onLoad() {
            if (this.refreshing) {
                this.list = [];
                this.refreshing = false;
            }
            await this.getListCoupon();
            this.coupons.push(...this.couponlist);
            this.page++;
            this.loading = false; //不加载了，滚动底会变为true
            if (this.coupons.length >= this.total) {
                this.finished = true; //数据全部加载完设为true
            }
        },
        onRefresh() {
            // 清空列表数据
            this.finished = false;
            // 重新加载数据
            // 将 loadingall 设置为 true，表示处于加载状态
            this.loading = true;
            this.page = 1;
            this.onLoad();
            this.$toast.success('刷新成功');
        },
        toOrderOne(id) {
            let domainname = this.$route.params.domainname;
            let FrontUrl = window.location.protocol + '//' + window.location.host;
            const nexturl = FrontUrl + `/xuejing/orderone/${domainname}?id=${id}`; //跳转到下一个页面的地址http://192.168.2.11/#/xuejing/order/test1?id=8
            let url = this.$front.getWeiXinOpenId + `?nexturl=${encodeURIComponent(nexturl)}`; //请求获得openid并存入到服务器的Cache当中
            window.location.href = url;
            //this.$router.push({ name: 'order', query: { id: this.id } });
        }
    },
    async created() {
        this.onLoad();
        this.getListNoCoupon();
    }
}

</script>
<style lang='less' scoped></style>
